package com.panda.tool.controller;

import com.alibaba.excel.EasyExcel;
import com.panda.tool.config.DDLFileConfigProperties;
import com.panda.tool.dto.ExcelFieldDTO;
import com.panda.tool.listener.ExcelToDDLListener;
import com.panda.tool.service.ToolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName ToolController
 * @Description TODO
 * @Author LD
 * @Date 2023-11-17 12:19:54
 * @Version 1.0
 */
@RequestMapping("/tool")
@RestController
public class ToolController {

    @Autowired
    private DDLFileConfigProperties fileConfigProperties;


    @PostMapping("/import/to/ddl")
    public Map<String,Object> importExcelToDDL(@RequestParam("files") MultipartFile[] files) throws IOException {
        for (MultipartFile file : files) {
            EasyExcel.read(file.getInputStream(), ExcelFieldDTO.class, new ExcelToDDLListener(fileConfigProperties))
                    .sheet()
                    // 多行头：从第二行开始是数据类
                    .headRowNumber(2)
                    .doRead();
        }

        // 结果
        String data = "DDL脚本文件已生成，请到【" + fileConfigProperties.getPath() + "】中查看！";
        // 返回对象
        Map<String,Object> resMap = new HashMap<>();
        resMap.put("code",0);
        resMap.put("msg","请求成功");
        resMap.put("data",data);

        return resMap;
    }


}
